package com.diet263.activity;

import android.app.PendingIntent;
import android.content.Intent;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.nfc.tech.Ndef;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.diet263.nfcleedemo.R;

public class RunApplicationActivity extends BaseActivity {

	private String mPackageName;
	private NfcAdapter mNfcAdapter;
	private PendingIntent mPendingIntent;
	private TextView tvSelectApp;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_auto_run_application);
		tvSelectApp = (TextView) findViewById(R.id.tv_selected_app_name);
		mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
		mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()), 0);
	}

	public void onNewIntent(Intent intent) {
		if (mPackageName == null) {
			Toast.makeText(this, "请先点击按钮选择要启动的应用程序", Toast.LENGTH_LONG).show();
			return;
		}

		Tag detectedTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
		writeNFCTag(detectedTag);
	}

	public void onResume() {
		super.onResume();
		if (mNfcAdapter != null)
			mNfcAdapter.enableForegroundDispatch(this, mPendingIntent, null, null);
	}

	public void onPause() {
		super.onPause();
		if (mNfcAdapter != null)
			mNfcAdapter.disableForegroundDispatch(this);
	}

	public void onClick_SelectAutoRunApplication(View view) {
		Intent intent = new Intent(this, InstalledApplicationListActivity.class);
		startActivityForResult(intent, 0);
	}

	public void writeNFCTag(Tag tag) {
		if (tag == null) {
			return;
		}
		NdefMessage ndefMessage = new NdefMessage(new NdefRecord[] { NdefRecord.createApplicationRecord(mPackageName) });
		int size = ndefMessage.toByteArray().length;
		try {
			Ndef ndef = Ndef.get(tag);
			if (ndef != null) {
				ndef.connect();

				if (!ndef.isWritable()) {
					return;
				}
				if (ndef.getMaxSize() < size) {
					return;
				}
				ndef.writeNdefMessage(ndefMessage);
				Toast.makeText(this, "ok", Toast.LENGTH_LONG).show();
				tvSelectApp.setText("");
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == 1) {
			tvSelectApp.setText(data.getExtras().getString("package_name"));
			String temp = tvSelectApp.getText().toString();
			mPackageName = temp.substring(temp.indexOf("\n") + 1);
		}
	}

}
